﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.WebFile.Admin
{
    public partial class _specialajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!CheckFormUrl())
            {
                Response.Write("提交有误");
                Response.End();
            }
            Admin_Load("0000", "json");
            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "ajaxDel":
                    ajaxDel();
                    break;
                case "ajaxCreateSpecial":
                    ajaxCreateSpecial();
                    break;
                case "checkname":
                    ajaxCheckName();
                    break;
                case "updatefore":
                    ajaxUpdateFore();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = JsonResult(0, "未知操作");
        }
        private void ajaxCheckName()
        {
            if (q("id") == "0")
            {
                doh.Reset();
                doh.ConditionExpress = "title=@title";
                doh.AddConditionParameter("@title", q("txtTitle"));
                if (doh.Exist("jcms_normal_special"))
                    this._response = JsonResult(0, "不可添加");
                else
                    this._response = JsonResult(1, "可以添加");
            }
            else
            {
                doh.Reset();
                doh.ConditionExpress = "title=@title and id<>" + q("id");
                doh.AddConditionParameter("@title", q("txtTitle"));
                if (doh.Exist("jcms_normal_special"))
                    this._response = JsonResult(0, "不可修改");
                else
                    this._response = JsonResult(1, "可以修改");
            }
        }
        private void ajaxGetList()
        {
            int page = Int_ThisPage();
            int PSize = Str2Int(q("pagesize"), 20);
            int countNum = 0;
            string sqlStr = "";
            string whereStr = "Id>0";//分页条件(不带A.)
            doh.Reset();
            doh.ConditionExpress = whereStr;
            countNum = doh.Count("jcms_normal_special");
            sqlStr = Jumbot.Common.staticFunction.GetSql("[ID],[Title],[Source]", "jcms_normal_special", "Id", PSize, page, "desc", whereStr);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt, (PSize * (page - 1))) +
                "}";
            dt.Clear();
            dt.Dispose();
        }
        private void ajaxDel()
        {
            string sId = f("id");
            doh.Reset();
            doh.ConditionExpress = "sId=@id";
            doh.AddConditionParameter("@id", sId);
            doh.Delete("jcms_normal_specialcontent");
            doh.Reset();
            doh.ConditionExpress = "id=@id";
            doh.AddConditionParameter("@id", sId);
            doh.Delete("jcms_normal_special");
            this._response = JsonResult(1, "成功删除");
        }
        private void ajaxCreateSpecial()
        {
            string sId = f("id");
            JumbotCms.DataObject.Special Special = new JumbotCms.DataObject.Special();
            Special.GetAllColumn(sId, doh);
            string PageStr = Jumbot.Common.DirFile.ReadFile("~/_data/special/_" + Special.Source);
            JumbotCms.API.TemplateEngine te = new JumbotCms.API.TemplateEngine(doh, "0");
            te.IsHtml = site.IsHtml;
            te.PageNav = "<a href=\"/\">首页</a>&nbsp;&gt;&nbsp;专题&nbsp;&gt;&nbsp;" + Special.Title;
            te.PageTitle = Special.Title + " - 专题 - " + site.Name + site.TitleTail;
            te.PageKeywords = site.Keywords;
            te.PageDescription = Jumbot.Common.Strings.SimpleLineSummary(Special.Info);
            te.ReplacePublicTag(ref PageStr, "0", te.DefaultSkinId);
            te.ReplaceSpecialTag(ref PageStr, sId);
            PageStr = PageStr.Replace("<@textarea ", "<textarea ");
            PageStr = PageStr.Replace("</textarea@>", "</textarea>");
            Jumbot.Common.DirFile.SaveFile(PageStr, site.Dir + "special/" + Special.Source);
            this._response = JsonResult(1, "生成成功");
        }
        private void ajaxUpdateFore()
        {
            CreateSpecialListPage();
            CreateSpecial();
            this._response = JsonResult(1, "更新完成");
        }
        /// <summary>
        /// 生成特约专题列表页
        /// </summary>
        private void CreateSpecialListPage()
        {
            string PageStr = Jumbot.Common.DirFile.ReadFile("~/templates/speciallist_index.htm");
            JumbotCms.API.TemplateEngine te = new JumbotCms.API.TemplateEngine(doh, "0");
            te.IsHtml = site.IsHtml;
            te.PageNav = "<a href=\"/\">首页</a>&nbsp;&gt;&nbsp;过往专题列表";
            te.PageTitle = "过往专题列表 - " + site.Name + site.TitleTail;
            te.PageKeywords = site.Keywords;
            te.PageDescription = site.Description;
            te.ReplacePublicTag(ref PageStr, "0", te.DefaultSkinId);
            Jumbot.Common.DirFile.SaveFile(PageStr, site.Dir + "speciallist.htm");
        }
        /// <summary>
        /// 生成特约专题json
        /// </summary>
        private void CreateSpecial()
        {
            string TempStr = "var $$_JSON_SpecialList = {";
            doh.Reset();
            doh.SqlCmd = "SELECT TOP 10 [Id],[Title],[Source] FROM [jcms_normal_special] ORDER BY Id DESC";
            DataTable dtSpecial = doh.GetDataTable();
            TempStr += "recordcount: " + dtSpecial.Rows.Count + ", " +
                "table: [";
            if (dtSpecial.Rows.Count > 0)
            {
                for (int i = 0; i < dtSpecial.Rows.Count; i++)
                {
                    if (i > 0) TempStr += ",";
                    TempStr += "{" +
                        "no: " + i + ", " +
                        "id: " + dtSpecial.Rows[i]["Id"].ToString() + ", " +
                        "title: '" + dtSpecial.Rows[i]["Title"].ToString() + "', " +
                        "source: '" + dtSpecial.Rows[i]["Source"].ToString() + "'" +
                        "}";
                }
            }
            TempStr += "]";
            TempStr += "}";
            dtSpecial.Clear();
            dtSpecial.Dispose();
            Jumbot.Common.DirFile.SaveFile(TempStr, "~/_data/json/_speciallist.js");
        }
    }
}